package com.oursnail.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created By XJL On 2017/11/4
 */
public class AppConsumer {

    private static final String url="tcp://localhost:61616";
    private static final String topicName="query-test";

    public static void main(String[] args) throws JMSException {
        //1、创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //2、创建Connection
       Connection connection = connectionFactory.createConnection();
       //3、打开连接
        connection.start();
        //4、创建会话
        final Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //5、创建目的地
        Destination destination = session.createTopic(topicName);
        //6、创建一个消费者
        MessageConsumer consumer = session.createConsumer(destination);
        //7、创建一个监听器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接受消息————"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }

            }
        });



    }
}
